package com.ttc.ssm.interceptor.home;

import com.ttc.ssm.controller.common;
import com.ttc.ssm.po.DeliveryActivity;
import com.ttc.ssm.po.InviteActivity;
import com.ttc.ssm.service.home.HomeCompanyService;
import com.ttc.ssm.service.home.HomeCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Jiarui on 2016/7/22.
 */
public class LoginInterceptor  implements HandlerInterceptor {

    @Autowired
    HomeCustomerService homeCustomerService;

    @Autowired
    HomeCompanyService homeCompanyService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //取得当前请求的url
        String url = request.getRequestURI();
        System.out.println("---------"+url);

        //如果是登录请求，不拦截
        if(url.indexOf("login.action")>0 || url.indexOf("logout.action")>0 || url.indexOf("loginPage.action")>0) {
            return true;
        }

        if (url.indexOf("jobDetail.action")>0 || url.indexOf("info_view.action")>0) {
            return true;
        }

        //取得session中的数据
        HttpSession session = request.getSession();
        Object name = session.getAttribute("fullname");

        //如果用户已经登录，不拦截
        if(name != null) {
            String type = (String) session.getAttribute("type");
            Map map = common.InterceptForType();
            Map temp = (Map) map.get(type);
            if(temp.get(url) != null)
                return true;
        }
        String redirect_url = "http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+request.getServletPath();
        if(request.getQueryString() != null){
            redirect_url+="?"+request.getQueryString();
        }
        session.setAttribute("redirect_url_home",redirect_url);

        //执行这里表示用户身份需要认证，跳转登陆页面,此处重定向有错误，必须请求转发
//		response.sendRedirect(request.getContextPath()+"/WEB-INF/jsp/login.jsp");
        request.getRequestDispatcher("/WEB-INF/jsp/home/login.jsp").forward(request, response);

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HttpSession session = request.getSession();
        String type = (String) session.getAttribute("type");
        String url = request.getRequestURI();
        if(type!= null && type.equals("0")){
            Map map = common.InterceptForType();
            Map temp = (Map) map.get(type);
            if(temp.get(url)!=null && temp.get(url).equals("1")) {
                ModelMap model = modelAndView.getModelMap();
                int customer_id = (Integer) session.getAttribute("customer_id");
                int noReadCountInvite = homeCustomerService.countRowsNoReadCustomer(customer_id);
                List<InviteActivity> inviteActivities = homeCustomerService.selectActivityCustomer(customer_id);
                int deliveryCount = homeCustomerService.countRowsNoReadCustomerDelivery(customer_id);
                List<DeliveryActivity> deliveryActivities = homeCustomerService.selectDeliveryActivityCustomer(customer_id);
                model.addAttribute("inviteActivities", inviteActivities);
                model.addAttribute("deliveryActivities", deliveryActivities);
                model.addAttribute("totalCount", noReadCountInvite + deliveryCount);
            }
        }
        if(type != null && type.equals("1")){
            Map map = common.InterceptForType();
            Map temp = (Map) map.get(type);
            if(temp.get(url)!=null && temp.get(url).equals("1")) {
                ModelMap model = modelAndView.getModelMap();
                int company_id = (Integer) session.getAttribute("company_id");
                int noReadCountInvite = homeCompanyService.countRowsNoReadCompany(company_id);
                List<InviteActivity> inviteActivities = homeCompanyService.selectActivityCompany(company_id);
                int deliveryCount = homeCompanyService.countRowsNoReadCompanyDelivery(company_id);
                List<DeliveryActivity> deliveryActivities = homeCompanyService.selectDeliveryActivityCompany(company_id);
                model.addAttribute("inviteActivities", inviteActivities);
                model.addAttribute("deliveryActivities", deliveryActivities);
                model.addAttribute("totalCount",  noReadCountInvite+deliveryCount);
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
